home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / SAT Invaders demo ƒ / StarField.p < prev   
Text File  |  1996-05-23  |  3KB  |  105 lines

  1. { Starfield unit for SAT Invaders.}
  2. {This unit uses the pixel array operations in SAT.}
  3. {Richard Bannister deserves credits for making a similar improved SAT Invaders,}
  4. {though using sprites for the stars. His demo convinced me that there should be}
  5. {single pixel operations in SAT. }
  6.  
  7. unit StarField;
  8. interface
  9.     uses
  10. {$IFC UNDEFINED THINK_PASCAL}
  11.         Types, Memory, QuickDraw,
  12. {$ENDC}
  13.         SAT, SATAddOnLib;
  14.  
  15. {procedure InitStars;}
  16.     procedure DoStars;
  17.     procedure ToggleStarField (starFieldFlag: Boolean);
  18.  
  19. implementation
  20.  
  21.     var
  22.         px, pxcopy: PixelPtr;
  23.     const
  24.         kNumPixels = 100;
  25.  
  26.     procedure InitStars;
  27.         var
  28.             i: Integer;
  29.     begin
  30.         px := PixelPtr(NewPtrClear(SizeOf(Pixel) * kNumPixels));
  31.         pxcopy := PixelPtr(NewPtrClear(SizeOf(Pixel) * kNumPixels));
  32.         for i := 0 to kNumPixels - 1 do
  33.             begin
  34.                 px^[i].data1 := 0; {SATRand(7) - 3}
  35.                 px^[i].data2 := SATRand(7) + 1;
  36.                 px^[i].position.h := SATRand(gSAT.offSizeH);
  37.                 px^[i].position.v := SATRand(gSAT.offSizeV);
  38.             end;
  39.     end; {InitStars}
  40.  
  41.     procedure ToggleStarField (starFieldFlag: Boolean);
  42.         var
  43.             savePort: SATPort;
  44.     begin
  45.         if starFieldFlag then
  46.             begin
  47.                 SATGetPort(savePort);
  48.                 SATSetPortBackScreen;
  49.                 ForeColor(blackColor);
  50.                 PaintRect(gSAT.backScreen.bounds);
  51.                 CopyBits(gSAT.backScreen.port^.portBits, gSAT.offScreen.port^.portBits, gSAT.backScreen.bounds, gSAT.backScreen.bounds, srcCopy, nil);
  52.                 SATRedraw;
  53.                 SATSetPort(savePort);
  54.             end
  55.         else
  56.             begin
  57.                 SATGetPort(savePort);
  58.                 SATDrawPICTs(129, 128);
  59.                 SATRedraw;
  60.                 SATSetPort(savePort);
  61.             end;
  62.     end; {ToggleStarField}
  63.  
  64. {The pixel set is processed wit the assumption that all pixels move every frame.}
  65. {They are drawn on top of the sprites.}
  66.  
  67.     procedure DoStars;
  68.         var
  69.             i: Integer;
  70.     begin
  71.         if px = nil then
  72.             InitStars;
  73.         BlockMove(Ptr(px), Ptr(pxcopy), GetPtrSize(Ptr(px)));        {Make a copy}
  74.         for i := 0 to kNumPixels - 1 do
  75.             begin
  76.                 px^[i].position.h := px^[i].position.h + px^[i].data1;
  77.                 px^[i].position.v := px^[i].position.v + px^[i].data2;
  78.  
  79.                 if px^[i].position.h < 0 then
  80.                     begin
  81.                         px^[i].position.h := 0;
  82.                         px^[i].data1 := Abs(px^[i].data1);
  83.                     end;
  84.                 if px^[i].position.v < 0 then
  85.                     begin
  86.                         px^[i].position.v := 0;
  87.                         px^[i].data2 := Abs(px^[i].data2);
  88.                     end;
  89.                 if px^[i].position.h >= gSAT.offSizeH then
  90.                     begin
  91.                         px^[i].position.h := gSAT.offSizeH - 1;
  92.                         px^[i].data1 := -Abs(px^[i].data1);
  93.                     end;
  94.                 if px^[i].position.v >= gSAT.offSizeV then
  95.                     begin
  96.                         px^[i].position.v := 0;
  97. {gSAT.offSizeV - 1;}
  98. {px^[i].data2 := -Abs(px^[i].data2);}
  99.                     end;
  100.             end;
  101.         SATDrawPixels(px, gSAT.wind, $ffffff00);                                {Draw in new positions}
  102.         SATCopyPixels(pxcopy, gSAT.offScreen, gSAT.wind);        {Erase old positions}
  103.     end; {DoStars}
  104.  
  105. end.